<?php
class Video_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

	function ajax_view_video()
	{
		$id = $this->input->post('id');
		$reff = $this->input->server('HTTP_REFERER');
		
		if(!empty($id) && preg_match('/'.$id.'/',$reff)):
			//UPDATE Video (View)
			$this->db->set('views', 'views + 1', FALSE);
			$this->db->where(array('video_id' => $id));
			$this->db->update('video');
			
			//Keep Stat View
			$datetime = date('Y-m-d');
			$is_exist = $this->db->from('video_click_stat')->where(array('video_id' => $id,'update_date' => $datetime))->count_all_results();
			if(!empty($is_exist)):
				$this->db->set('video_click_views', 'video_click_views + 1', FALSE);
				$this->db->where(array('video_id' => $id,'update_date' => $datetime));
				$this->db->update('video_click_stat');
			else:
				$data = array(
					'video_id' => $id,
					'video_click_views' => 1,
					'update_date' => $datetime
				);
				$this->db->insert('video_click_stat',$data);
			endif;		
		endif;
		exit;		
	}
	
	function ajax_vote_video()
	{
		$video_id = $this->input->post('video_id');
		$score = $this->input->post('score');
		$datetime = date('Y-m-d');
		$dataMember = $this->session->userdata('member');
		if(!empty($dataMember)):
			$member_id = $dataMember['member_id'];
			$is_exist = $this->db->from('video_rating')->where(array('video_id' => $video_id,'member_id' => $member_id))->count_all_results();
			if(!empty($is_exist)):
				//Output
				$data = array(
					'msg' => 'คุณเคยให้คะแนนเพลงนี้ไปแล้วค่ะ',
					'avg' => '',
					'people' => ''
				);			
				echo json_encode($data);
			else:
				$data = array(
					'video_id' => $video_id,
					'member_id' => $member_id,
					'video_rating_score' => $score
				);
				//video_rating
				$this->db->insert('video_rating',$data);
				//video_click_stat
				$this->db->set('video_click_rate', 'video_click_rate + 1', FALSE);
				$this->db->where(array('video_id' => $video_id,'update_date' => $datetime));
				$this->db->update('video_click_stat');
				//video
				$avg = $this->db->select('SUM(video_rating_score)/COUNT(video_rating_score) as Avg')->group_by('video_id')->get_where('video_rating',array('video_id' => $video_id))->row()->Avg;
				$this->db->update('video',array('rate_score' => $avg),array('video_id' => $video_id));
				//Output
				$data = array(
					'msg' => 'ให้คะแนนเพลงนี้เรียบร้อยแล้วค่ะ',
					'avg' => number_format($avg,2),
					'people' => $this->db->from('video_rating')->where(array('video_id' => $video_id))->count_all_results()
				);
				echo json_encode($data);
			endif;		
		else:
			//Output
			$data = array(
				'msg' => 'กรุณาล๊อคอินก่อนโหวตคะแนนค่ะ',
				'avg' => '',
				'people' => ''
			);			
			echo json_encode($data);			
		endif;
		exit;
	}
	
	function ajax_copy_stat()
	{
		$video_id = $this->input->post('video_id');
		$datetime = date('Y-m-d');
		$this->db->set('video_click_embed', 'video_click_embed + 1', FALSE);
		$this->db->where(array('video_id' => $video_id,'update_date' => $datetime));
		$this->db->update('video_click_stat');		
		exit;
	}
}

?>